using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.Configuration;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string currentFile = System.IO.Path.GetFileName(HttpContext.Current.Request.Url.AbsolutePath).ToLowerInvariant();
        if(currentFile != "install.aspx") {
            if(WebConfigurationManager.AppSettings["salt"].Length == 0) { // no salt, first time non-webPI installation
                Response.Redirect("~/Install.aspx");
            } else if(WebConfigurationManager.AppSettings["webpi"] == "true") { //  first time Web PI installation
                Database db = null;
                try {
                    db = new Database();
                    db.OpenConnection();
                    using(Database.Command cmd = db.CreateCommand()) {
                        cmd.CommandText = string.Format("SELECT password FROM {0}users WHERE permissions = -1", db.TablePrefix);
                        object res = cmd.ExecuteScalar();
                        if(res != null) { // user created by WebPI installer needs password salted
                            Response.Redirect("~/Install.aspx");
                        }
                    }
                } catch(Exception ex)  {
                    throw new ResourceBlenderException(ex.Message);
                }
            }
        }
        switch(Request.FilePath.Remove(0, Request.FilePath.LastIndexOf('/')).ToLowerInvariant()) {
            case "/applications.aspx":
                lnkHelp.NavigateUrl = "http://wiki.lime49.com/index.php?title=Adding_an_Application_%28ResourceBlender.NET%29";
                break;
            case "/backup.aspx":
                lnkHelp.NavigateUrl = "http://wiki.lime49.com/index.php?title=Backup_%26_Restore_%28ResourceBlender.NET%29";
                break;
            case "/createvariant.aspx":
                lnkHelp.NavigateUrl = "http://wiki.lime49.com/index.php?title=Creating_Variants_%28ResourceBlender.NET%29";
                break;
            case "/duplicateresources.aspx":
                lnkHelp.NavigateUrl = "http://wiki.lime49.com/index.php?title=Duplicate_Resources";
                break;
            case "/generate.aspx":
                lnkHelp.NavigateUrl = "http://wiki.lime49.com/index.php?title=Exporting_to_a_Useable_format";
                break;
            case "/import.aspx":
                lnkHelp.NavigateUrl = "http://wiki.lime49.com/index.php?title=Importing_from_existing_resource_files";
                break;
            case "/install.aspx":
                lnkHelp.NavigateUrl = "http://wiki.lime49.com/index.php?title=Installation_%28ResourceBlender.NET%29";
                break;
            case "/machinetranslate.aspx":
                lnkHelp.NavigateUrl = "http://wiki.lime49.com/index.php?title=Automatic_Translation";
                break;
            case "/resources.aspx":
            case "/resourcesexcel.aspx":
            case "/resourcesonline.aspx":
            case "/resourcesummary.aspx":
            case "/excel.aspx":
                lnkHelp.NavigateUrl = "http://wiki.lime49.com/index.php?title=Translating_an_Application_%28ResourceBlender.NET%29";
                break;
            case "/templates.aspx":
                lnkHelp.NavigateUrl = "http://wiki.lime49.com/index.php?title=Templates_%28ResourceBlender.NET%29";
                break;
            case "/unusedresources.aspx":
                lnkHelp.NavigateUrl = "http://wiki.lime49.com/index.php?title=Unused_Resources";
                break;
            default:
                lnkHelp.NavigateUrl = "http://wiki.lime49.com/index.php?title=ResourceBlender";
                break;
        }
    }
    /// <summary>
    /// Outputs a JavaScript array with server and client IDs and a GetClientId method which allows obtaining a client ID from a server ID
    /// </summary>
    /// <param name="wc">The wc.</param>
    public void RenderJSArrayWithCliendIds(params Control[] wc) {
        if(wc.Length > 0) {
            StringBuilder arrClientIDValue = new StringBuilder();
            StringBuilder arrServerIDValue = new StringBuilder();
            ClientScriptManager cs = Page.ClientScript;
            for(int i = 0; i < wc.Length; i++) {
                arrClientIDValue.Append("\"" + wc[i].ClientID + "\",");
                arrServerIDValue.Append("\"" + wc[i].ID + "\",");
            }
            // Register the array of client and server id to the client
            cs.RegisterArrayDeclaration("clientIDS", arrClientIDValue.ToString().Remove(arrClientIDValue.ToString().Length - 1, 1));
            cs.RegisterArrayDeclaration("serverIDs", arrServerIDValue.ToString().Remove(arrServerIDValue.ToString().Length - 1, 1));
            cs.RegisterStartupScript(this.Page.GetType(), "key", @"
            function GetClientId(serverId) {
              for(i=0; i<serverIDs.length; i++) {
                if(serverIDs[i] == serverId) {
                  return clientIDS[i];
            } } }"+'\n', true);
        }
    }
}
